/* ==================================================================
* Copyright (c) 2024/11/15 SCNU LiuJunJie z-ken
* All rights reserved.
* fileName:	analyseRecord.cpp
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the xxx Group. and
* its contributors.
* 4. Neither the name of the Group nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Author,GROUP AND CONTRIBUTORS
* ===================================================================
* Author: LiuJunJie z-ken.
*/
//
// Created by zken on 2024/11/4.
//

#include "analyseRecord.h"

analyseRecord::analyseRecord() {

}

analyseRecord::analyseRecord(stack<int> status, stack<AnalyseValue> characterStatus,
                             queue<AnalyseValue> inputQueueStatus) {
    this->statusStackStatus = util::stackToString(status, 1);


    this->characterStackStatus = AnalyseValue::stackToString(characterStatus, 1);
    this->inputQueueStatus = AnalyseValue::queueToString(inputQueueStatus);
}

string analyseRecord::getDetails() {
    string detail = to_string(lineId) + "\t" + statusStackStatus + "\t" + characterStackStatus +
                    "\t" + inputQueueStatus + "\t" + action + "\t" + gotoContent;
    return detail;
}

string AnalyseValue::stackToString(stack<AnalyseValue> s, int reverse1) {
    string res = "";
    vector<string> v;
    // 拷贝
    stack<AnalyseValue> t = stack<AnalyseValue>(s);
    while (!t.empty()) {
        string s_t = t.top().tokenType;
        t.pop();
        v.push_back(s_t);
    }
    if (reverse1) {
        reverse(v.begin(), v.end());
    }
    for (const string &vi: v) {
        res = res + (vi);
        res += ",";
    }

    return res;
}

string AnalyseValue::queueToString(queue<AnalyseValue> q) {
    {
        string res = "";
        queue<AnalyseValue> t = queue<AnalyseValue>(q);
        while (!t.empty()) {
            string i = t.front().tokenType;
            t.pop();
            res = res + i;
            res += ",";
        }
        return res;
    }
}

AstTreeNode *AstTree::makeLeaf(AnalyseValue value) {
    AstTreeNode *newLeaf = new AstTreeNode();
    newLeaf->value = value.tokenContent;
//    newLeaf->value = value.tokenType + ":" + value.tokenContent;
    return newLeaf;
}

AstTreeNode *AstTree::doAttribute(int productionId, vector<AnalyseValue> values) {
    if (productionId >= productionUtil1->productions.size()) {
        throw AST_TREE_ERROR();
    }
    production p_t = productionUtil1->productions[productionId];
    if (p_t.right.size() == 1 && p_t.right[0] == productionUtil1->CEIGEMA && values.size() == 0) {
        cout << "遭遇空节点，并且规约的右部内容长度为0，不检测values的内容个数" << endl;
    } else {
        if (p_t.right.size() != values.size()) {
            throw AST_TREE_ERROR();
        }
    }


    try {
        if (!this->productionsName.empty()) {
            cout << this->productionsName << "-AST execute: " << productionId << endl;
        }
        if (this->productionsName == "exp") {
            return doAttribute_exp(productionId, values);
        } else if (this->productionsName == "tiny") {
            return doAttribute_tiny(productionId, values);
        } else if (this->productionsName == "minic") {
            return doAttribute_minic(productionId, values);
        }
    }
    catch (...) {
        // 注意C++的抛出语法是对象语法，不是指针语法
        throw AST_TREE_ERROR();
    }

    return nullptr;
}

void AstTree::showAstTree() {
    if (this->root == nullptr) {
        cout << "AST树节点为空，亦或者是未进行硬编码语法树的生成" << endl;
        return;
    }
    try {
        showTreeNodesSupport(this->root, 0);
    } catch (...) {
        throw AST_TREE_ERROR();
    }
}

void AstTree::showTreeNodesSupport(AstTreeNode *root, int depth) {
    cout << string(depth, '.') << root->value << endl;
    for (int i = 0; i < root->children.size(); i++) {
        AstTreeNode *childNode = root->children[i];
        showTreeNodesSupport(childNode, depth + 1);
    }
}


// 规定productionUtil1中的productionId=0是扩展文法
// exp文法的属性文法
AstTreeNode *AstTree::doAttribute_exp(int productionId, vector<AnalyseValue> values) {
    AstTreeNode *astTreeNode = nullptr;

    if (productionId == 1) { //exp -> exp addop term
        // addop对应的值在第二位，exp的值对应着addop的加号
        AstTreeNode *addOpTreeNode = values.at(1).ptr;
        // 加入addop左边的exp指针
        addOpTreeNode->children.push_back(values.at(0).ptr);
        // 加入term
        addOpTreeNode->children.push_back(values.at(2).ptr);
        astTreeNode = addOpTreeNode;
    } else if (productionId == 2) { // exp -> term
        // 继承term的ptr
        AstTreeNode *expTreeNode = values.at(0).ptr;
        astTreeNode = expTreeNode;
    } else if (productionId == 3) {
//        addop -> +
        AstTreeNode *addOPTreeNode = makeLeaf(values.at(0));
        astTreeNode = addOPTreeNode;
    } else if (productionId == 4) {
//        addop -> -
        AstTreeNode *addOPTreeNode = makeLeaf(values.at(0));
        astTreeNode = addOPTreeNode;
    } else if (productionId == 5) {
        //term -> term mulop factor
        AstTreeNode *mulOpTreeNode = values.at(1).ptr;
        // 加入addop左边的exp指针
        mulOpTreeNode->children.push_back(values.at(0).ptr);
        // 加入term
        mulOpTreeNode->children.push_back(values.at(2).ptr);
        astTreeNode = mulOpTreeNode;
    } else if (productionId == 6) {
        //term -> factor
        AstTreeNode *termTreeNode = values.at(0).ptr;
        astTreeNode = termTreeNode;
    } else if (productionId == 7) {
//        mulop -> *
        AstTreeNode *mulOpTreeNode = makeLeaf(values.at(0));
        astTreeNode = mulOpTreeNode;
    } else if (productionId == 8) {
//        mulop -> /
        AstTreeNode *mulOpTreeNode = makeLeaf(values.at(0));
        astTreeNode = mulOpTreeNode;
    } else if (productionId == 9) {
//        factor -> ( exp )
        AstTreeNode *lpeanTreeNode = values.at(1).ptr;
        astTreeNode = lpeanTreeNode;
    } else if (productionId == 10) {
//factor -> n
        AstTreeNode *nTreeNode = makeLeaf(values.at(0));
        astTreeNode = nTreeNode;
    }
    return astTreeNode;
}


// 进行tiny的属性操作构造，构造语法树节点
AstTreeNode *AstTree::doAttribute_tiny(int productionId, vector<AnalyseValue> values) {
    AstTreeNode *returnTreeNode = nullptr;
    if (productionId == 1) {
        //program -> stmt-sequence
        AstTreeNode *programNode = new AstTreeNode();
        programNode->value = "program";
        programNode->children.push_back(values.at(0).ptr);
        returnTreeNode = programNode;
    } else if (productionId == 2) {
        //stmt-sequence -> stmt-sequence _seg statement
        AstTreeNode *statementSquenceNode = values.at(0).ptr;
        // 加入另一个statement
        statementSquenceNode->children.push_back(values.at(2).ptr);
        returnTreeNode = statementSquenceNode;
    } else if (productionId == 3) {
        //stmt-sequence -> statement
        AstTreeNode *statementSquenceNode = new AstTreeNode();
        statementSquenceNode->value = "stmt-sequence";
        statementSquenceNode->children.push_back(values.at(0).ptr);
        returnTreeNode = statementSquenceNode;
    }
        // 为了好看，statement下的所有的stmt都主动加入xx-stmt的一个节点
    else if (productionId == 4) {
        //statement -> if-stmt
        // 为了好看，加入if-stmt的一个节点
        AstTreeNode *statementNode = new AstTreeNode();
        statementNode->value = values.at(0).tokenType;
        // 加入if的根节点指针
        statementNode->children.push_back(values.at(0).ptr);
        returnTreeNode = statementNode;
    } else if (productionId == 5) {
        //statement -> repeat-stmt
        AstTreeNode *statementNode = new AstTreeNode();
        statementNode->value = values.at(0).tokenType;
        // 加入repeat语法的指针
        statementNode->children.push_back(values.at(0).ptr);
        returnTreeNode = statementNode;
    } else if (productionId == 6) {
        //statement -> assign-stmt
        AstTreeNode *statementNode = new AstTreeNode();
        statementNode->value = values.at(0).tokenType;
        // 加入repeat语法的指针
        statementNode->children.push_back(values.at(0).ptr);
        returnTreeNode = statementNode;
    } else if (productionId == 7) {
        //statement -> read-stmt
        AstTreeNode *statementNode = new AstTreeNode();
        statementNode->value = values.at(0).tokenType;
        // 加入repeat语法的指针
        statementNode->children.push_back(values.at(0).ptr);
        returnTreeNode = statementNode;
    } else if (productionId == 8) {
        //statement -> write-stmt
        AstTreeNode *statementNode = new AstTreeNode();
        statementNode->value = values.at(0).tokenType;
        // 加入repeat语法的指针
        statementNode->children.push_back(values.at(0).ptr);
        returnTreeNode = statementNode;
    } else if (productionId == 9) {
        //if-stmt -> _if exp _then stmt-sequence _end
        AstTreeNode *ifNode = makeLeaf(values.at(0));
        // 加入exp的指针
        ifNode->children.push_back(values.at(1).ptr);
        // 加入then的指针
        ifNode->children.push_back(makeLeaf(values.at(2)));
        // 加入 stmt-sequence 的指针
        ifNode->children.push_back(values.at(3).ptr);
        // 不加入end的指针value[4]
        returnTreeNode = ifNode;
    } else if (productionId == 10) {
        // if-stmt -> _if exp _then stmt-sequence _else stmt-sequence _end
        // 创建if节点
        AstTreeNode *ifNode = makeLeaf(values.at(0));
        // 加入exp的指针
        ifNode->children.push_back(values.at(1).ptr);
        // 加入then的指针
        ifNode->children.push_back(makeLeaf(values.at(2)));
        // 加入 stmt-sequence 的指针
        ifNode->children.push_back(values.at(3).ptr);
        // 此文法的end在第4位
        // 让else成为一个子节点，当然，你也可以记住此处else的位置，在if-stmt的归于为statement的时候，将then上提，这也是个不错的方法
        AstTreeNode *elseNode = makeLeaf(values.at(4));
        // 加入else 后方法的stmt-sequence指针
        elseNode->children.push_back(values.at(5).ptr);
        // 将elseNode加入ifnode的子节点的第四位
        ifNode->children.push_back(elseNode);
        // 不加入end的指针(掠过value【6】
        returnTreeNode = ifNode;
    } else if (productionId == 11) {
        //repeat-stmt -> _repeat stmt-sequence _until exp
        AstTreeNode *repeatNode = makeLeaf(values.at(0));
        repeatNode->children.push_back(values.at(1).ptr);
        AstTreeNode *untilTreeNode = makeLeaf(values.at(2));
        // 加入exp的指针
        untilTreeNode->children.push_back(values.at(3).ptr);
        // 将until加入到repeat的子节点
        repeatNode->children.push_back(untilTreeNode);
        returnTreeNode = repeatNode;
    } else if (productionId == 12) {
        //assign-stmt -> _id _assign exp
        AstTreeNode *assignNode = makeLeaf(values.at(1));
        AstTreeNode *idNode = makeLeaf(values.at(0));
        assignNode->children.push_back(idNode);
        // 加入exp表达式的根节点指针
        assignNode->children.push_back(values.at(2).ptr);
        returnTreeNode = assignNode;
    } else if (productionId == 13) {
        //read-stmt -> _read _id
        AstTreeNode *readNode = makeLeaf(values.at(0));
        AstTreeNode *idNode = makeLeaf(values.at(1));
        readNode->children.push_back(idNode);
        returnTreeNode = readNode;
    } else if (productionId == 14) {
        // write-stmt -> _write exp
        AstTreeNode *writeNode = makeLeaf(values.at(0));
        writeNode->children.push_back(values.at(1).ptr);
        returnTreeNode = writeNode;
    } else if (productionId == 15) {
        // exp -> simple-exp comparison-op simple-exp
        // 将运算符号的指针赋值给exp的树节点
        AstTreeNode *expTreeNode = values.at(1).ptr;
        // 加入两个simple-exp
        expTreeNode->children.push_back(values.at(0).ptr);
        expTreeNode->children.push_back(values.at(2).ptr);
        returnTreeNode = expTreeNode;
    } else if (productionId == 16) {
        //exp -> simple-exp
        AstTreeNode *expTreeNode = values.at(0).ptr;
        returnTreeNode = expTreeNode;
    } else if (productionId == 17) {
        // comparison-op -> _lp
        AstTreeNode *comparisonOpNode = makeLeaf(values.at(0));
        returnTreeNode = comparisonOpNode;
    } else if (productionId == 18) {
        // comparison-op -> _rp
        AstTreeNode *comparisonOpNode = makeLeaf(values.at(0));
        returnTreeNode = comparisonOpNode;
    } else if (productionId == 19) {
        //comparison-op -> _beq
        AstTreeNode *comparisonOpNode = makeLeaf(values.at(0));
        returnTreeNode = comparisonOpNode;
    } else if (productionId == 20) {
        //comparison-op -> _lpe
        AstTreeNode *comparisonOpNode = makeLeaf(values.at(0));
        returnTreeNode = comparisonOpNode;
    } else if (productionId == 21) {
        //comparison-op -> _neq
        AstTreeNode *comparisonOpNode = makeLeaf(values.at(0));
        returnTreeNode = comparisonOpNode;
    } else if (productionId == 22) {
        //comparison-op -> _rpe
        AstTreeNode *comparisonOpNode = makeLeaf(values.at(0));
        returnTreeNode = comparisonOpNode;
    } else if (productionId == 23) {
        //simple-exp -> simple-exp addop term
        // 将addop作为根节点
        AstTreeNode *simpleExpNode = values.at(1).ptr;
        // 加入simple-exp和term
        simpleExpNode->children.push_back(values.at(0).ptr);
        simpleExpNode->children.push_back(values.at(2).ptr);
        returnTreeNode = simpleExpNode;
    } else if (productionId == 24) {
        //simple-exp -> term
        // 上提节点内容
        AstTreeNode *simpleExpNode = values.at(0).ptr;
        returnTreeNode = simpleExpNode;
    } else if (productionId == 25) {
        //addop -> _add
        AstTreeNode *addOpNode = makeLeaf(values.at(0));
        returnTreeNode = addOpNode;
    } else if (productionId == 26) {
        //addop -> _sub
        AstTreeNode *addOpNode = makeLeaf(values.at(0));
        returnTreeNode = addOpNode;
    } else if (productionId == 27) {
        // term -> term mulop factor
        AstTreeNode *termNode = values.at(1).ptr;
        termNode->children.push_back(values.at(0).ptr);
        termNode->children.push_back(values.at(2).ptr);
        returnTreeNode = termNode;
    } else if (productionId == 28) {
        //term -> factor
        //上提一层
        AstTreeNode *termNode = values.at(0).ptr;
        returnTreeNode = termNode;
    }
        /*
         * 赋值粘贴重复的代码没有意义，在这里将mulop -> _mul | _div | _mod | _power打包书写，对应的
         * 文法代码分别是29.30.31.32
         */
    else if (productionId == 29
             || productionId == 30
             || productionId == 31
             || productionId == 32) {
        AstTreeNode *mulOpNode = makeLeaf(values.at(0));
        returnTreeNode = mulOpNode;
    } else if (productionId == 33) {
        // factor -> _LPEAN exp _RPEAN
        // 上提一层
        AstTreeNode *factorTreeNode = values.at(1).ptr;
        returnTreeNode = factorTreeNode;
    } else if (productionId == 34) {
        //factor -> _num
        // 创建叶子后返回
        returnTreeNode = makeLeaf(values.at(0));
    } else if (productionId == 35) {
        //factor -> _id
        // 创建叶子后返回
        returnTreeNode = makeLeaf(values.at(0));
    }
    return returnTreeNode;
}

AstTreeNode *AstTree::doAttribute_minic(int productionId, vector<AnalyseValue> values) {
    AstTreeNode *returnTreeNode = nullptr;

    if (productionId == 1) {
        // program -> definition-list
        AstTreeNode *programNode = new AstTreeNode();
        programNode->value = "program";
        programNode->children.push_back(values.at(0).ptr);
        returnTreeNode = programNode;
    } else if (productionId == 2) {
        //definition-list -> definition-list definition
        // 继承之前的list
        AstTreeNode *definitionListNode = values.at(0).ptr;
        // 加入后来的definition
        definitionListNode->children.push_back(values.at(1).ptr);
        returnTreeNode = definitionListNode;
    } else if (productionId == 3) {
        // definition-list -> definition
        // 创建definition-list
        AstTreeNode *definitionListNode = new AstTreeNode();
        definitionListNode->value = "definition-list";
        // 加入被规约的 儿子节点
        definitionListNode->children.push_back(values.at(0).ptr);
        returnTreeNode = definitionListNode;
    } else if (productionId == 4) {
        //definition -> variable-definition
//        继承定义
        AstTreeNode *definitionNode = values.at(0).ptr;
        returnTreeNode = definitionNode;
    } else if (productionId == 5) {
        // definition -> function-definition
        // 继承定义
        AstTreeNode *definitionNode = values.at(0).ptr;
        returnTreeNode = definitionNode;
    } else if (productionId == 6) {
        // variable-definition -> type-indicator _id _seg
        // 将type进行提升
        AstTreeNode *variableDefinitionNode = values.at(0).ptr;
        // 将id加入儿子节点
        variableDefinitionNode->children.push_back(makeLeaf(values.at(1)));
        returnTreeNode = variableDefinitionNode;
    } else if (productionId == 7) {
        //variable-definition -> type-indicator _id _LMPEAN _num _RMPEAN _seg
        // 将type进行提升
        AstTreeNode *variableDefinitionNode = values.at(0).ptr;
        // 将id加入儿子节点
        variableDefinitionNode->children.push_back(makeLeaf(values.at(1)));
        // 将左右中括号[] 和 _num 加入儿子节点
        variableDefinitionNode->children.push_back(makeLeaf(values.at(2)));
        variableDefinitionNode->children.push_back(makeLeaf(values.at(3)));
        variableDefinitionNode->children.push_back(makeLeaf(values.at(4)));
        returnTreeNode = variableDefinitionNode;
    }
        /*
         * type-indicator ->  _int  |  _float  |  _double | _void
         * 这四个的id对应的是8 9 10 11
         */
    else if (productionId == 8
             || productionId == 9
             || productionId == 10
             || productionId == 11) {
        // type-indicator -> _int  |  _float  |  _double | _void
        // 继承提升"类型"
        AstTreeNode *typeNode = makeLeaf(values.at(0));
        returnTreeNode = typeNode;
    } else if (productionId == 12) {
        // function-definition -> type-indicator _id _LPEAN parameters _RPEAN compound-stmt
        // 将type进行继承，保留函数定义的根设置为id
        AstTreeNode *functionDefinitionNode = makeLeaf(values.at(1));
        // 稍微扩展function的信息
        functionDefinitionNode->value = "function-" + functionDefinitionNode->value;
        // 加入类型
        functionDefinitionNode->children.push_back(values.at(0).ptr);
        // 加入参数
        functionDefinitionNode->children.push_back(values.at(3).ptr);
        // 加入函数体
        functionDefinitionNode->children.push_back(values.at(5).ptr);
        returnTreeNode = functionDefinitionNode;
    } else if (productionId == 13) {
        // parameters -> parameter-list
        // 继承list
        // TODO parameters 这里可能不继承子节点List
        AstTreeNode *parametersNode = values.at(0).ptr;
        returnTreeNode = parametersNode;
    } else if (productionId == 14) {
        // parameters -> _void
        //直接 将内容设置为叶子void
        AstTreeNode *parametersNode = makeLeaf(values.at(0));
        returnTreeNode = parametersNode;
    } else if (productionId == 15) {
        //parameter-list -> parameter-list _spl parameter
        //继承list，然后将parameter加入为子节点
        AstTreeNode *parametersListNode = values.at(0).ptr;
        // 加入parameter
        parametersListNode->children.push_back(values.at(2).ptr);
        returnTreeNode = parametersListNode;
    } else if (productionId == 16) {
        //parameter-list -> parameter
        // 建立一个新的list作为父亲节点,然后加入parameter
        AstTreeNode *parametersListNode = new AstTreeNode();
        parametersListNode->value = "parameter-list";
        parametersListNode->children.push_back(values.at(0).ptr);
        returnTreeNode = parametersListNode;
    } else if (productionId == 17) {
        //parameter -> type-indicator _id
        // 继承type作为根
        AstTreeNode *parametersNode = values.at(0).ptr;
        // 加入变量id
        parametersNode->children.push_back(makeLeaf(values.at(1)));
        returnTreeNode = parametersNode;
    } else if (productionId == 18) {
        // parameter -> type-indicator _id _LMPEAN _RMPEAN
        AstTreeNode *parametersNode = values.at(0).ptr;
        // 加入变量id 和左右中括号
        parametersNode->children.push_back(makeLeaf(values.at(1)));
        parametersNode->children.push_back(makeLeaf(values.at(2)));
        parametersNode->children.push_back(makeLeaf(values.at(3)));
        returnTreeNode = parametersNode;
    } else if (productionId == 19) {
        //compound-stmt -> _LLPEAN local-definitions statement-list _RLPEAN
        // 创建一个compound-stmt父节点
        AstTreeNode *compoundStmtNode = new AstTreeNode();
        compoundStmtNode->value = "compound-stmt:{}";
        // local-definitions statement-list 加入为子节点
        compoundStmtNode->children.push_back(values.at(1).ptr);
        compoundStmtNode->children.push_back(values.at(2).ptr);
        returnTreeNode = compoundStmtNode;
    } else if (productionId == 20) {
        //local-definitions -> local-definitions variable-definition
        // 继承local-definitions
        AstTreeNode *localDefNode = values.at(0).ptr;
        // 加入variable-definition为子节点
        localDefNode->children.push_back(values.at(1).ptr);
        returnTreeNode = localDefNode;
    } else if (productionId == 21) {
        // local-definitions -> @
        // 空规约，value传入的情况是空
        //直接创建一个空结点，最后可以决定这个空结点要不要计入到其中
        AstTreeNode *localDefNode = new AstTreeNode();
        localDefNode->value = "local-definitions";
        // 暂时不加入空儿子进去。
        // 返回设置
        returnTreeNode = localDefNode;
    } else if (productionId == 22) {
        // statement-list -> statement-list statement
        // 继承list，加入statement
        AstTreeNode *statementListNode = values.at(0).ptr;
        // 加入statement节点
        statementListNode->children.push_back(values.at(1).ptr);
        returnTreeNode = statementListNode;
    } else if (productionId == 23) {
        // statement-list -> @
        // 和definitionList同理，要不要加空看你喜欢，反正这里先创建一个没有儿子的父亲并且返回
        AstTreeNode *statementListNode = new AstTreeNode();
        statementListNode->value = "statement-list";
        returnTreeNode = statementListNode;
    }
        /*
         * statement -> expression-stmt  |  compound-stmt  |  condition-stmt  | while-stmt  | dowhile-stmt  |  return-stmt
         * 这些规约式都直接让statement进行继承即可
         * 编号为24 25 26 27 28 29
         */
    else if (productionId == 24
             || productionId == 25
             || productionId == 26
             || productionId == 27
             || productionId == 28
             || productionId == 29) {
        // 让statement继承子节点
        AstTreeNode *statementNode = values.at(0).ptr;
        returnTreeNode = statementNode;
    } else if (productionId == 30) {
        //expression-stmt -> expression _seg
        // 保留左部的expression-stmt，创建根节点
        AstTreeNode *expNode = new AstTreeNode();
        expNode->value = "expression-stmt";
        // 将expression加入子节点
        expNode->children.push_back(values.at(0).ptr);
        returnTreeNode = expNode;
    } else if (productionId == 31) {
        // expression-stmt -> _seg
        // 单独读取一个分号要怎么办呢？，那就单独保留一个分号
        AstTreeNode *expNode = new AstTreeNode();
        expNode->value = "expression-stmt";
        // 将分割符加入子节点
        expNode->children.push_back(makeLeaf(values.at(0)));
        returnTreeNode = expNode;
    } else if (productionId == 32) {
        //condition-stmt -> _if _LPEAN expression _RPEAN statement _seg
        // 创建if
        AstTreeNode *conditionStmt = makeLeaf(values.at(0));
        // expression
        conditionStmt->children.push_back(values.at(2).ptr);
        // statement
        conditionStmt->children.push_back(values.at(4).ptr);
        returnTreeNode = conditionStmt;
    } else if (productionId == 33) {
        //condition-stmt -> if _LPEAN expression _RPEAN statement else statement _seg
        // 创建if，作为condition-stmt的根节点
        AstTreeNode *conditionStmt = makeLeaf(values.at(0));
        // expression
        conditionStmt->children.push_back(values.at(2).ptr);
        // statement
        conditionStmt->children.push_back(values.at(4).ptr);
        // 创建else
        AstTreeNode *elseStmt = makeLeaf(values.at(5));
        elseStmt->children.push_back(values.at(6).ptr);
        // 将else插入到if内
        conditionStmt->children.push_back(elseStmt);
        returnTreeNode = conditionStmt;
    } else if (productionId == 34) {
        // while-stmt -> _while _LPEAN expression _RPEAN statement _seg
        AstTreeNode *whileNode = makeLeaf(values.at(0));
        // 加入exp和statement
        whileNode->children.push_back(values.at(2).ptr);
        whileNode->children.push_back(values.at(4).ptr);
        returnTreeNode = whileNode;
    } else if (productionId == 35) {
        //dowhile-stmt -> _do statement _while _LPEAN expression _RPEAN _seg
        AstTreeNode *doNode = makeLeaf(values.at(0));
        // 加入statement
        doNode->children.push_back(values.at(1).ptr);
        AstTreeNode *whileNode = makeLeaf(values.at(2));
        // 加入expression
        whileNode->children.push_back(values.at(4).ptr);
        // 将while作为child加入
        doNode->children.push_back(whileNode);
        returnTreeNode = doNode;
    } else if (productionId == 36) {
        // return-stmt -> _return _seg
        AstTreeNode *returnNode = makeLeaf(values.at(0));
        returnTreeNode = returnNode;
    } else if (productionId == 37) {
        // return-stmt -> _return expression _seg
        AstTreeNode *returnNode = makeLeaf(values.at(0));
        returnNode->children.push_back(values.at(1).ptr);
        returnTreeNode = returnNode;
    } else if (productionId == 38) {
        //expression -> variable _assgin expression
        AstTreeNode *expressionNode = makeLeaf(values.at(1));
        expressionNode->children.push_back(values.at(0).ptr);
        expressionNode->children.push_back(values.at(2).ptr);
        returnTreeNode = expressionNode;
    } else if (productionId == 39) {
        //expression -> simple-expression
        //继承
        AstTreeNode *expressionNode = values.at(0).ptr;
        returnTreeNode = expressionNode;
    } else if (productionId == 40) {
        // variable -> _id
        AstTreeNode *variableNode = makeLeaf(values.at(0));
        returnTreeNode = variableNode;
    } else if (productionId == 41) {
        //variable -> _id _LMPEAN expression _RMPEAN
        AstTreeNode *variableNode = makeLeaf(values.at(0));
        // 左中括号[
        variableNode->children.push_back(makeLeaf(values.at(1)));
        // exp
        variableNode->children.push_back(values.at(2).ptr);
        // ]
        variableNode->children.push_back(makeLeaf(values.at(3)));
        returnTreeNode = variableNode;
    } else if (productionId == 42) {
        //simple-expression -> additive-expression relop additive-expression
        // 将relop设置为根节点
        AstTreeNode *simpleExpNode = values.at(1).ptr;
        // 加入additive-expression
        simpleExpNode->children.push_back(values.at(0).ptr);
        simpleExpNode->children.push_back(values.at(2).ptr);
        returnTreeNode = simpleExpNode;
    } else if (productionId == 43) {
        //simple-expression -> additive-expression
        // 直接继承值
        AstTreeNode *simpleExpNode = values.at(0).ptr;
        returnTreeNode = simpleExpNode;
    }
        /*
         * relop -> _lpe | _lp | _rp | _rpe | _beq | _neq
         * 这个文法的id从 44 45 46 47 48 49
         */
    else if (productionId == 44
             || productionId == 45
             || productionId == 45
             || productionId == 46
             || productionId == 47
             || productionId == 48
             || productionId == 49) {
        // relop -> _lpe | _lp | _rp | _rpe | _beq | _neq
        // 直接 make叶子节点,进行返回即可
        returnTreeNode = makeLeaf(values.at(0));
    } else if (productionId == 50) {
        //additive-expression -> additive-expression addop term
        // 将addop设置为根节点
        AstTreeNode *addExpNode = values.at(1).ptr;
        addExpNode->children.push_back(values.at(0).ptr);
        addExpNode->children.push_back(values.at(2).ptr);
        returnTreeNode = addExpNode;
    } else if (productionId == 51) {
        //additive-expression -> term
        // 直接继承
        AstTreeNode *addExpNode = values.at(0).ptr;
        returnTreeNode = addExpNode;
    }
        /*
         *addop -> _add | _minus
         * index编号为52 53
         */
    else if (productionId == 52
             || productionId == 53) {
        returnTreeNode = makeLeaf(values.at(0));
    } else if (productionId == 54) {
        //term -> term mulop factor
        AstTreeNode *termNode = values.at(1).ptr;
        termNode->children.push_back(values.at(0).ptr);
        termNode->children.push_back(values.at(2).ptr);
        returnTreeNode = termNode;
    } else if (productionId == 55) {
        // term -> factor
        // 直接继承返回
        AstTreeNode *termNode = values.at(0).ptr;
        returnTreeNode = termNode;
    }
        /*
         * mulop -> _mul | _div | _mod  | _power
         *  对应的id为
         *  56 57 58 59
         */
    else if (productionId == 56
             || productionId == 57
             || productionId == 58
             || productionId == 59) {
        // 直接将符号节点返回即可
        returnTreeNode = makeLeaf(values.at(0));
    } else if (productionId == 60) {
        //factor -> _LPEAN expression _RPEAN
        AstTreeNode *factorNode = values.at(1).ptr;
        returnTreeNode = factorNode;
    } else if (productionId == 61) {
        //factor -> variable
        // 继承返回
        AstTreeNode *factorNode = values.at(0).ptr;
        returnTreeNode = factorNode;
    } else if (productionId == 62) {
        // factor -> call
        // 继承返回
        AstTreeNode *factorNode = values.at(0).ptr;
        returnTreeNode = factorNode;
    } else if (productionId == 63) {
        //factor -> _num
        // 创建num
        AstTreeNode *factorNode = makeLeaf(values.at(0));
        returnTreeNode = factorNode;
    } else if (productionId == 64) {
        //call -> _id _LPEAN arguments _RPEAN
        AstTreeNode *callNode = makeLeaf(values.at(0));
        // 补充一点信息
        callNode->value = "call:" + callNode->value;
        // 加入参数到child
        callNode->children.push_back(values.at(2).ptr);
        returnTreeNode = callNode;
    } else if (productionId == 65) {
        //arguments -> argument-list
        //形式一: 创建一个arguments节点 -- 实际上不需要
        AstTreeNode *argumentsNode = new AstTreeNode();
        argumentsNode->value = "arguments";
        // 加入child
        argumentsNode->children.push_back(values.at(0).ptr);
        // 形式二:
//        AstTreeNode *argumentsNode = values.at(0).ptr;
        // 返回节点值
        returnTreeNode = argumentsNode;
    } else if (productionId == 66) {
        //arguments -> @
        // 返回一个没有儿子的argumentsNode
        AstTreeNode *argumentsNode = new AstTreeNode();
        argumentsNode->value = "arguments";
        returnTreeNode = argumentsNode;
    } else if (productionId == 67) {
        //argument-list -> argument-list _spl expression
        // 继承argument-list,然后加入exp为儿子
        AstTreeNode *listNode = values.at(0).ptr;
        listNode->children.push_back(values.at(2).ptr);
        returnTreeNode = listNode;
    } else if (productionId == 68) {
        // argument-list -> expression
        // 创建一个新的argument-list父节点,然后加入expression
        AstTreeNode *listNode = new AstTreeNode();
        listNode->value = "argument-list";
        listNode->children.push_back(values.at(0).ptr);
        returnTreeNode = listNode;
    }
    return returnTreeNode;
}

void AstTree::setProductionUtil(ProductionUtil *util) {
    this->productionUtil1 = util;
}
